package com.herench.bs.entrance;

import com.alibaba.fastjson.JSON;
import com.herench.bs.support.MessageBuilder;
import com.herench.bs.support.MessageProcessor;
import com.herench.bs.support.RequestMessage;
import com.herench.bs.support.ResponseMessage;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;


/**
 * Created by ThinkPad on 2017/8/14.
 */

@CrossOrigin(origins = "*", maxAge = 3600)
@Controller
@RequestMapping("/entrance")
public class HttpEntrance {

    private static final Logger logger = Logger.getLogger(HttpEntrance.class);

    @Autowired
    private MessageProcessor processor;

    
    @RequestMapping("/platformService")
    @ResponseBody
    public ResponseMessage platformService(@RequestBody final String originalMsg) {
        ResponseMessage res = null;
        RequestMessage requestMessage = new RequestMessage(originalMsg);
        logger.info("originalMsg:"+requestMessage);
        try {
            requestMessage = MessageBuilder.decoration(requestMessage);
            res = processor.handler(requestMessage);
        } catch (Exception e) {
            res = MessageBuilder.builderErrResMsg(requestMessage, e.getMessage());
            e.printStackTrace();
        }
        logger.info("res:"+ JSON.toJSONString(res));
        return res;
    }

}
